@extends('admin.layouts.app')  
@section('css')
<link rel="stylesheet" href="{{ asset('assets/plugins/treegrid/css/jquery.treegrid.css')}}">
<link rel="stylesheet" href="{{ asset('assets/css/documentselecttree.min.css') }}">
<link rel="stylesheet" href="{{ asset('assets/plugins/select2/css/select2.min.css') }}" />

<!-- Sweet Alert -->
<link href="{{ asset('assets/plugins/bootstrap-sweetalert/sweet-alert.css')}}" rel="stylesheet" type="text/css">

@endsection
@section('content')
<!--begin right-->
<div class="container">
    

    <section class="content">
        <div class="row">
            <div class="col-xs-12">
                <div class="box">
                    <div class="box-header">
                        <h3 class="box-title">菜单列表</h3> &nbsp&nbsp&nbsp
                            <span>
                              <!--添加-->
                              
                              <a href="{{ url('zadmin/menu/create') }}" class="btn btn-primary">添加</a>
                              
                            </span>
                       
                    </div>
                    <!-- /.box-header -->
                    <div class="card-box">
                    
                        <table id="example2" class="table  table-striped tree">
                            <thead>
                            <tr>
                                
                                <th>菜单名称</th>
                                <th>链接</th>
                                <th>排序</th>
                                <th>创建时间</th>
                                <th>操作</th>
                            </tr>
                            </thead>
                            <tbody>
                            @foreach($list as $k=>$data)

                                <tr class="treegrid-{{$data->id}} {{$data->parent_id?'treegrid-parent-'.$data->parent_id:''}}">
                                    <td><label><input type="checkbox" value="{{ $data->id }}"></label> &nbsp 
                                   {{ $data->name }}</td>
                                    <td>{{ $data->url}}</td>
                                    <td>{{ $data->weight}}</td>
                                    <td>{{ $data->created_at }}</td>
                                    <td>
                                    @if($data->subMenu->isEmpty())    
                                    <a href="{{url('zadmin/menu/permission',$data->id)}}">权限</a> &nbsp;
                                    @endif
                                    <a href="#" data-toggle="modal" data-target="#subModal" data-parent_id="{{$data->id}}" data-parent_level="{{ $data->level }}">子菜单</a> &nbsp;

                                    
                                        <a href="#" data-toggle="modal" data-target="#myModal3" data-id="{{ $data->id }}">编辑</a>
                                        &nbsp;
                                        <a href="#" class="delete-alert" data-id="{{ $data->id }}"> 删除</a>
                                    </td>
                                </tr>
                            @endforeach
                            </tbody>
                        </table>

                    </div>
                    
                  
                    <!-- 模态框（编辑） -->
                    <div class="modal fade" id="myModal3"  role="dialog" aria-labelledby="myModalLabel"
                         aria-hidden="true">
                        <div class="modal-dialog">
                            <div class="modal-content">
                                <form method="post" id="update-form">
                                    {{ csrf_field() }}
                                    {{ method_field('PUT') }}
                                <div class="modal-header">
                                    <button type="button" class="close" data-dismiss="modal"
                                            aria-hidden="true">&times;</button>
                                    <h4 class="modal-title" id="myModalLabel">编辑菜单</h4>
                                </div>
                                <!--内容-->
                                <div class="modal-body motaikuang-tianjia">
                                    <div class="form-group">
                                <label>菜单名称</label>
                                <input type="text" class="form-control" placeholder="菜单名" name="name">
                                </div>
                                <div class="form-group">
                                    <label>URL</label>
                                    <input type="text" class="form-control" placeholder="URL" name="url">
                                </div>
                                <div class="form-group">
                                    <label>排序</label>
                                    <input type="text" class="form-control" placeholder="排序数字" value="" name="weight">
                                </div>
                                <div class="form-group">
                                    <label>icon小图标</label>
                                    <input type="text" class="form-control" placeholder="小图标" name="icon">
                                </div>
                                <div class="form-group">
                                    <label>上级菜单</label><br/>
                                    <select class="form-control select2" style="width: 200px;" name="parent_id">
                                    <option value="0">顶级菜单</option>
                                    @foreach($menus as $menu)
                                        <option value="{{$menu->id}}">{{$menu->name}}</option>   
                                    @endforeach
                                    </select>
                                </div>
                                    <p>
                                        <button type="button" class="btn bg-olive btn-flat margin btn-update">保存</button>
                                        <button type="button" class="btn bg-orange btn-flat margin" data-dismiss="modal">取消 </button>
                                    </p>
                                </div>
                                <input type="hidden" name="id" />
                                </form>
                            </div>
                        </div>
                    </div>
                    <!-- end 模态框（编辑） -->

                    <!-- 子菜单模态框（添加） -->
                    <div class="modal fade" id="subModal" tabindex="1" role="dialog" aria-labelledby="myModalLabel"
                         aria-hidden="true">
                        <div class="modal-dialog">
                            <div class="modal-content">
                                <form method="post" action="{{ url('zadmin/menu') }}" id="submenu-form">
                                    {{ csrf_field() }}
                                   
                                <div class="modal-header">
                                    <button type="button" class="close" data-dismiss="modal"
                                            aria-hidden="true">&times;</button>
                                    <h4 class="modal-title" id="myModalLabel">添加子菜单</h4>
                                </div>
                                <!--内容-->
                                <div class="modal-body motaikuang-tianjia">
                                    <div class="form-group">
                                <label>菜单名称</label>
                                <input type="text" class="form-control" placeholder="菜单名" name="name">
                                </div>
                                <div class="form-group">
                                    <label>URL</label>
                                    <input type="text" class="form-control" placeholder="URL" name="url">
                                </div>
                                <div class="form-group">
                                    <label>排序</label>
                                    <input type="text" class="form-control" placeholder="排序数字" value="{{$weight}}" name="weight">
                                </div>
                                <div class="form-group">
                                    <label>icon小图标</label>
                                    <input type="text" class="form-control" placeholder="小图标" name="icon">
                                </div>
                                    <input type="hidden" name="parent_id" />
                                    <input type="hidden" name="parent_level" />
                                    
                                    <p>
                                        <button type="submit" class="btn bg-olive btn-flat margin">保存</button>
                                        <button type="button" class="btn bg-orange btn-flat margin" data-dismiss="modal">取消 </button>
                                    </p>
                                </div>
                                
                                </form>
                            </div>
                        </div>
                    </div>
                    <!-- end 模态框（编辑） -->


                </div>
                <!-- /.box -->
            </div>
            <!-- /.col -->
        </div>
    </section>
</div>
<!--end right-->
@endsection
@section('js')
<script type="text/javascript" src="{{ asset('assets/plugins/treegrid/js/jquery.treegrid.js')}}"></script>
<script type="text/javascript" src="{{ asset('assets/plugins/treegrid/js/jquery.treegrid.bootstrap3.js')}}"></script>
 <script src="{{ asset('assets/plugins/select2/js/select2.full.min.js') }}" type="text/javascript"></script>

<!-- Sweet-Alert  -->
<script src="{{ asset('assets/plugins/bootstrap-sweetalert/sweet-alert.min.js')}}"></script>

<script>
        // init select2
        $(".select2").select2();


  

        $(function () {
            $('.tree').treegrid();
            // 添加
            $("#subModal").on("show.bs.modal", function(e) {
                var button = $(e.relatedTarget);
                var _id    = button.data("parent_id");                
                var _level    = button.data("parent_level");
                var modal  = $(this).find(".modal-body");
                //console.info(_id);
                modal.find("input[name=parent_id]").val(_id);                
                modal.find("input[name=parent_level]").val(_level);
            });

            $("#myModal3").on("show.bs.modal", function(e) {
                var button = $(e.relatedTarget);
                var _id = button.data("id");
                
                var modal = $(this).find(".modal-body");
                $.ajax({
                    url: "{{url('zadmin/menu')}}/"+_id+'/edit',
                    type: "GET",
                    dataType: "json",
                    success: function(data) {
                        if (data.rs == "true") {
                            $("input[name=id]").val(_id);  
                            modal.find("input[name=name]").val(data.data.name);
                            modal.find("input[name=url]").val(data.data.url);
                            modal.find("input[name=weight]").val(data.data.sort);
                            modal.find("input[name=icon]").val(data.data.icon);
                            modal.find("select[name=parent_id]").val(data.data.parent_id);
                        } else {
                            swal("查询失败", data.msg, "error")
                        }
                    },
                    error: function() {
                        swal("查询失败", "请重新尝试", "error")
                    }
                })
            });

            $(".delete-alert").on("click", function() {
                var _id = $(this).data("id");
                var token = "{{ csrf_token()  }}";
                var dom = $(this).parents("tr");
                swal({
                    title: "您确定要删除此菜单吗？",
                    text: "删除的菜单不可恢复",
                    type: "warning",
                    showCancelButton: true,
                    confirmButtonColor: "#DD6B55",
                    confirmButtonText: "确定",
                    cancelBUttonText: "取消",
                    closeOnConfirm: false,
                    closeOnCancel: true
                }, function(isConfirm) {
                    if (isConfirm) {
                        $.ajax({
                            url: "{{ url('zadmin/menu') }}/" + _id,
                          
                            data: {                               
                                _token: token,                               
                            },
                            type: "DELETE",
                            dataType: "json",
                            success: function(data) {
                                if (data.rs == "true") {
                                    dom.remove();
                                    swal("删除成功", "你选择的菜单已删除", "success")
                                } else {
                                    swal("删除失败", data.msg, "error")
                                }
                            },
                            error: function() {
                                swal("删除失败", "请刷新重新尝试!", "error")
                            }
                        })
                    }
                })
            });

            $('.btn-update').on('click',function(){
                var _id = $('input[name=id]').val();
                var _data = $('#update-form').serialize();
                $.ajax({
                    url: "{{url('zadmin/menu')}}/" +_id,
                    type: "PUT",
                    dataType: "json",
                    data:_data,
                    success: function(data) {
                        if (data.rs == "true") {
                            swal("操作成功", "", "success")
                           setTimeout(function(){  
                            window.location.reload(true);
                        },2000);
                        } else {
                            swal("操作失败", data.msg, "error")
                        }
                        $("#myModal3").modal('hide');
                    },
                    error: function() {
                        swal("操作失败", "请重新尝试", "error")
                    }
                })       
            } );
        });
        @if (count($errors) > 0)
         swal('', "{{ $errors->first() }}", 'warning');
        @endif
    </script>
@stop